﻿/*
* QTools工具函数
* 界面的也暂时放在这里
*/
var iterate = function(obj) { s = "遍历对象" + obj + "的属性：\n"; for (i in obj) s += i + ":" + obj[i] + "\n"; return s; };
var $id = function(n) {
    if ('string' == typeof n) {
        return document.getElementById(n);
    }
    return n;
};
var $name = function(n) {
    if ('string' == typeof n) {
        return document.getElementsByName(n);
    }
    return n;
};
var $select = function(n) {
    if ('string' == typeof n) {
        var d = document.getElementById(n);
        return d.options[d.selectedIndex].value;
    }
    return n;
};

var QUI = {
toggleRight: function(isblock) {
if (typeof (isblock) == "undefined") {
            isblock = $id('cont').style.display;
        }
        if ('none' == isblock) {
            $id('cont').style.display = 'block';
            //srcDom.className = 'shouqi1';
            $("#aShowHideSearchResult em")[0].className = "";
            $id('mapObj').style.width = (localwidth - 500) + "px";
            $id('divShouqi').style.right =  "500px";

        } else {
            $id('cont').style.display = 'none';
            //srcDom.className = 'shouqi';
            $("#aShowHideSearchResult em")[0].className = "zhank";
            $id('mapObj').style.width = (localwidth - 5) + "px";
            $id('divShouqi').style.right = "0px";
        }
    }
};
var QTools = {
    //取页面元素相对窗口的绝对位置
    getAbsPoint: function(e) {
        var x = e.offsetLeft;
        var y = e.offsetTop;
        while (e = e.offsetParent) {
            x += e.offsetLeft;
            y += e.offsetTop;
        }
        return { 'x': x, 'y': y };
    },
    //str 模板内容
    //obj 替换用的对象，简单的属性值对，如 {'key1':'val1','key2':'val2'}，会把模板中所有的 {$key1}、{$key2} 替换成 {val1}、{val2}
    templateFetch: function(str, obj) {
        var retval = str;
        //其实是顺次对 str 执行正则表达式的替换
        for (i in obj) {
            var re = new RegExp('\\{' + i + '\\}', 'g');
            retval = retval.replace(re, obj[i]);
        }
        return retval;
    },
    eventAdd: function(element, type, listener) {
        if (!element) {
            return;
        }
        type = type.replace(/^on/i, '').toLowerCase();
        if (element.addEventListener) {
            element.addEventListener(type, listener, false);
        } else if (element.attachEvent) {
            element.attachEvent("on" + type, listener);
        }
    },
    removeEvent: function(element, type, listener) {
        if (!element) {
            return;
        }
        type = type.replace(/^on/i, '').toLowerCase();
        if (element.removeEventListener) {
            element.removeEventListener(type, listener, false);
        } else if (element.detachEvent) {
            element.detachEvent("on" + type, listener);
        }
    },
    setA: function(itemid, val) {
        //		alert(itemid);
        if (!$id(itemid)) return;
        $id(itemid).innerHTML = val;
    },
    setInput: function(itemid, val) {
        //	alert(itemid);
        var formitem = $id(itemid);
        if (!formitem) return;
        formitem.value = val;
        //如果输入格里有提示文字，就有提示的样式“inputHint”，设定了值
        //formitem.className='';
        //因为有了点击文档任何地主都会隐藏菜单，所以设定值时就不用再额外隐藏菜单了。
        //this.hide(event);
    },
    /*
    普通的一个 a 配一个 input 的设置。
    参数:
    itemid 是 input 的 ID
    val 设给 input 的值
    hint 设给 a 的显示文字
    */
    setInputA: function(itemid, val, hint) {
        this.setInput(itemid, val);
        this.setA(itemid + '_a', hint);
    }
};
var SFXHR = function(url, method, params, onComplete, onFailure, loading) {
    var me = this;
    var getHTTPObject = function() {
        var xmlhttp = false;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    xmlhttp = false;
                }
            }
        }
        return xmlhttp;
    };

    var completeRequest = function() {
        if (me.XHR.readyState == 4) {
            if (me.XHR.status == 200 || me.XHR.status == 304) {
                if (onComplete) {
                    onComplete(me.XHR);
                }
            }
            else {
                if (onFailure) {
                    onFailure(me.XHR)
                };
            }
        }
    };

    var process = function() {
        if (loading) {
            loading();
        }
        var query = '';
        for (var i in params) {
            query += i + '=' + params[i] + '&';
        }
        me.XHR.onreadystatechange = completeRequest;
        if ('post' == me.method) {
            me.XHR.open("POST", url, true);
            me.XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            me.XHR.send(query);
        }
        else {
            url += '?' + query + 'random=' + Math.random();
            me.XHR.open("GET", url, true);
            me.XHR.send(null);
        }
    };
    me.XHR = getHTTPObject();
    me.method = ('get' == method.toLowerCase()) ? 'get' : 'post';
    process();
};